package org.dromara.project.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.project.domain.bo.ProjectInfoBo;
import org.dromara.project.domain.vo.ProjectInfoVo;
import org.dromara.project.domain.ProjectInfo;
import org.dromara.project.mapper.ProjectInfoMapper;
import org.dromara.project.service.IProjectInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目信息Service业务层处理
 *
 * @author Lion Li
 * @date 2025-06-23
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ProjectInfoServiceImpl implements IProjectInfoService {

    private final ProjectInfoMapper baseMapper;

    /**
     * 查询项目信息
     *
     * @param id 主键
     * @return 项目信息
     */
    @Override
    public ProjectInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询项目信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 项目信息分页列表
     */
    @Override
    public TableDataInfo<ProjectInfoVo> queryPageList(ProjectInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProjectInfo> lqw = buildQueryWrapper(bo);
        Page<ProjectInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的项目信息列表
     *
     * @param bo 查询条件
     * @return 项目信息列表
     */
    @Override
    public List<ProjectInfoVo> queryList(ProjectInfoBo bo) {
        LambdaQueryWrapper<ProjectInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProjectInfo> buildQueryWrapper(ProjectInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProjectInfo> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ProjectInfo::getId);
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), ProjectInfo::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getConstructionLocation()), ProjectInfo::getConstructionLocation, bo.getConstructionLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getOwnerUnit()), ProjectInfo::getOwnerUnit, bo.getOwnerUnit());
        lqw.eq(bo.getContractAmount() != null, ProjectInfo::getContractAmount, bo.getContractAmount());
        lqw.eq(bo.getSigningDate() != null, ProjectInfo::getSigningDate, bo.getSigningDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCustomerEmail()), ProjectInfo::getCustomerEmail, bo.getCustomerEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectStatus()), ProjectInfo::getProjectStatus, bo.getProjectStatus());
        lqw.eq(bo.getProjectManagerId() != null, ProjectInfo::getProjectManagerId, bo.getProjectManagerId());
        lqw.like(StringUtils.isNotBlank(bo.getProjectManagerName()), ProjectInfo::getProjectManagerName, bo.getProjectManagerName());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), ProjectInfo::getRemarks, bo.getRemarks());
        lqw.eq(StringUtils.isNotBlank(bo.getRefReason()), ProjectInfo::getRefReason, bo.getRefReason());
        lqw.eq(StringUtils.isNotBlank(bo.getApprover()), ProjectInfo::getApprover, bo.getApprover());
        lqw.like(bo.getApprovername() != null, ProjectInfo::getApprovername, bo.getApprovername());
        lqw.eq(bo.getApproveTime() != null, ProjectInfo::getApproveTime, bo.getApproveTime());
        return lqw;
    }

    /**
     * 新增项目信息
     *
     * @param bo 项目信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ProjectInfoBo bo) {
        ProjectInfo add = MapstructUtils.convert(bo, ProjectInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改项目信息
     *
     * @param bo 项目信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ProjectInfoBo bo) {
        ProjectInfo update = MapstructUtils.convert(bo, ProjectInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProjectInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除项目信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
